﻿using System;
using System.Globalization;
using Monkey.Warrenty.Biz;
using Monkey.Warrenty.EntityModel;

namespace MonkeyWarranty.Web.InstantTickets
{
    public partial class EditService : System.Web.UI.Page
    {
        #region Properties

        private InstantTicket_Service _ticketService;
        private InstantTicket_Service TicketService
        {
            get
            {
                if (_ticketService == null)
                {
                    if (TicketServiceId > 0)
                    {
                        _ticketService = InstantTicketManager.GetTicketServiceById(TicketServiceId);
                    }
                }
                return _ticketService;
            }
        }

        protected long TicketId
        {
            get { return (TicketService != null) ? TicketService.InstantTicketId : 0; }
        }

        private Service _service;
        protected Service CurrentService
        {
            get
            {
                if (_service == null)
                {
                    if (TicketService != null)
                    {
                        _service = ServiceManager.GetById(TicketService.ServiceId);
                    }
                }
                return _service;
            }
        }

        private long _ticketServiceId;
        private long TicketServiceId
        {
            get
            {
                if (_ticketServiceId <= 0) long.TryParse(Request.Params["id"], out _ticketServiceId);
                return _ticketServiceId;
            }
        }

        private static ServiceBiz _serviceBiz;
        private static ServiceBiz ServiceManager
        {
            get { return _serviceBiz ?? (_serviceBiz = new ServiceBiz()); }
        }

        private static InstantTicketBiz _instantTicketBiz;
        private static InstantTicketBiz InstantTicketManager
        {
            get { return _instantTicketBiz ?? (_instantTicketBiz = new InstantTicketBiz()); }
        }

        private static CategoryBiz _categoryBiz;
        private static CategoryBiz CategoryManager
        {
            get { return _categoryBiz ?? (_categoryBiz = new CategoryBiz()); }
        }

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (TicketServiceId <= 0 || TicketService == null)
                {
                    lblErrorMessage.Text = Resources.GlobalStrings.ServiceNotExisted;
                    btnSave.Enabled = false;
                    return;
                }
                LoadData();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            lblErrorMessage.Text = string.Empty;

            // validate inputs
            var serviceId = hidServiceId.Value;
            if (string.IsNullOrEmpty(serviceId))
            {
                lblErrorMessage.Text = Resources.GlobalStrings.ServiceIsRequired;
                return;
            }
            var descrition = txtDescription.Text;
            var note = txtNote.Text;

            TicketService.ServiceId = int.Parse(serviceId);
            TicketService.Description = descrition;
            TicketService.Note = note;

            int quantity;
            if (int.TryParse(txtQuantity.Text.Trim(), out quantity)) TicketService.Quantity = quantity;

            int serviceTypeId;
            if (int.TryParse(ddlServiceTypes.SelectedValue, out serviceTypeId)) TicketService.ServiceTypeId = serviceTypeId;

            int errorId;
            if (int.TryParse(ddlErrors.SelectedValue, out errorId)) TicketService.ErrorId = errorId;

            // Call Biz Save Product
            InstantTicketManager.UpdateTicketService(TicketService);

            lblErrorMessage.Text = Resources.GlobalStrings.UpdateSuccess;
        }

        #endregion

        #region Helpers

        private void LoadData()
        {
            // ticket service
            hidServiceId.Value = TicketService.ServiceId.ToString(CultureInfo.InvariantCulture);
            if (CurrentService != null)
            {
                txtServiceName.Text = CurrentService.Name;
            }
            txtQuantity.Text = TicketService.Quantity.ToString(CultureInfo.InvariantCulture);
            txtDescription.Text = TicketService.Description;
            txtNote.Text = TicketService.Note;

            // warranty types
            var serviceTypes = CategoryManager.GetAllServiceTypes();
            serviceTypes.Insert(0, new ServiceType { Id = 0, Name = string.Empty });
            ddlServiceTypes.DataTextField = "Name";
            ddlServiceTypes.DataValueField = "Id";
            ddlServiceTypes.DataSource = serviceTypes;
            ddlServiceTypes.DataBind();
            ddlServiceTypes.SelectedValue = string.Format("{0}", TicketService.ServiceTypeId);

            // errors
            var errors = CategoryManager.GetAllErrors();
            errors.Insert(0, new Error { Id = 0, Name = string.Empty });
            ddlErrors.DataTextField = "Name";
            ddlErrors.DataValueField = "Id";
            ddlErrors.DataSource = errors;
            ddlErrors.DataBind();
            ddlErrors.SelectedValue = string.Format("{0}", TicketService.ErrorId);
        }

        #endregion
    }
}